package com.hospital.register.core.receiver;

import com.hospital.register.common.exception.BusinessException;
import com.hospital.register.common.rabbit.constant.MqConst;
import com.hospital.register.common.rabbit.service.RabbitService;
import com.hospital.register.common.result.ResultCodeEnum;
import com.hospital.register.core.service.ScheduleService;
import com.hospital.register.vo.core.common.ScheduleQueryVo;
import com.hospital.register.vo.order.OrderMessageVo;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class HospitalReceiver {

    @Autowired
    private RabbitService rabbitService;

    @Autowired
    private ScheduleService scheduleService;

    /**
     * 记录日志
     */
    private final Logger logger = LoggerFactory.getLogger(HospitalReceiver.class);

    @RabbitListener(bindings = @QueueBinding(value = @Queue(value = MqConst.QUEUE_ORDER, durable = "true"), exchange = @Exchange(value = MqConst.EXCHANGE_DIRECT_ORDER), key = {
            MqConst.ROUTING_ORDER }))
    public void receive(OrderMessageVo orderMessage, Message message, Channel channel) {
        // 排班id会为空，不明原因
        try {
            logger.info("监听到修改浩源消息，消息内容：" + orderMessage);
            // 修改浩源、发送 MQ 消息
            if (orderMessage == null) {
                throw new BusinessException("消息内容为空", ResultCodeEnum.DATA_ERROR.getCode());
            }

            // 更新浩源
            if (orderMessage.getScheduleId() == null || orderMessage.getScheduleId().isEmpty()) {// 不明原因
                throw new BusinessException("排班 id 为空", ResultCodeEnum.DATA_ERROR.getCode());
            }

            ScheduleQueryVo schedule = this.scheduleService.findById(orderMessage.getScheduleId());
            if (schedule == null) {
                throw new BusinessException("下单修改浩源时，查询排班失败，订单信息：" + orderMessage,
                        ResultCodeEnum.PARAM_ERROR.getCode());
            }

            if (null == orderMessage.getAvailableNumber() || null == orderMessage.getReservedNumber()) {
                // 取消预约更新预约数
                int availableNumber = schedule.getAvailableNumber().intValue() + 1;
                orderMessage.setAvailableNumber(availableNumber);

                int reservedNumber = schedule.getReservedNumber().intValue() + 1;
                orderMessage.setReservedNumber(reservedNumber);
            }

            if (this.scheduleService.updateNumberById(orderMessage)) {// 更新浩源成功
                // 发送短信
                rabbitService.sendMessage(MqConst.EXCHANGE_DIRECT_MSM, MqConst.ROUTING_MSM_ITEM, orderMessage);
            }
        } catch (Exception e) {
            logger.error(orderMessage.toString());
            e.printStackTrace();
        }
    }

}